﻿@model QuanLyGiaoVuUI.Models.QuanLySinhVien.NhapBangDiemViewModel

@{
    Layout = null;
}

<style>
    .hiddenField {
        display: none;
    }
</style>
@{
    var nguoiDung = QuanLyGiaoVuUI.Extensions.Utilities.UserUtility.GetCurrentUser();
    bool laGiaoVuKhoa = (nguoiDung!= null && nguoiDung.VaiTro == 0);
    }
<script src="@Url.Content("~/Scripts/jquery.form.js")"></script>
@{using (Ajax.BeginForm("NhapBangDiemDialog", "QuanLySinhVien", new AjaxOptions
  {
      HttpMethod = "POST",
      OnSuccess = "OnSuccessCallBack"
  }))
  {
    @Html.HiddenFor(m => m.MaGiangVien)
    <table border="0" cellpadding="0" cellspacing="0" id="id-form" class="form">
        <tr>
            <th class="formLabel">@Html.LabelFor(model => model.MaLopSinhVien)</th>
            <td>@Html.DropDownListFor(model => model.MaLopSinhVien, new SelectList(Model.DanhSachLopSinhVien, "MaLop", "TenLop"))</td>
            <td>@Html.ValidationMessageFor(model => model.MaLopSinhVien)</td>
        </tr>
        <tr>
            <th class="formLabel">@Html.LabelFor(model => model.MaHocKy)</th>
            <td>@Html.DropDownListFor(model => model.MaHocKy, new SelectList(Model.DanhSachHocKy, "MaHocKy", "TenHocKyDayDu"))</td>
            <td>@Html.ValidationMessageFor(model => model.MaLopSinhVien)</td>
        </tr>
        <tr>
            <th class="formLabel">@Html.LabelFor(model => model.MaLopMonHoc)</th>
            <td>
                <a href="javascript:;" id="TenLopMonHoc">Chọn</a>
                @Html.HiddenFor(model => model.MaLopMonHoc)
            </td>
            <td>
                @Html.ValidationMessageFor(model => model.MaLopMonHoc)
            </td>
        </tr>
        <tr>
                                <td>
                                    <div class="editor-label">
                                        @Html.CheckBoxFor(m => m.TuyChinh)
                                        @Html.LabelFor(m => m.TuyChinh)
                                    </div>
                                </td>
                                <td>
                                    <div class="editor-field">
                                    </div>
                                </td>
                            </tr>
        <tr class="setting">
            <th class="formLabel">@Html.LabelFor(model => model.StartRowIndex)</th>
            <td>@Html.TextBoxFor(model => model.StartRowIndex, new { @class = "large" })
                 @if (laGiaoVuKhoa)
            {
                    <a href="javascript:;" class="btnSave" data-name="@QuanLyGiaoVuService.Utilities.ThamSoConstantUtility.NHAPDIEMDONGBATDAU" data-fieldName="StartRowIndex">Lưu làm mặc định</a>
            }
            </td>
            <td>@Html.ValidationMessageFor(model => model.StartRowIndex)</td>
           
        </tr>
        <tr class="setting">
            <th class="formLabel">@Html.LabelFor(model => model.EndRowIndex)</th>
            <td>@Html.TextBoxFor(model => model.EndRowIndex, new { @class = "large" })
                 @if (laGiaoVuKhoa)
            {
                    <a href="javascript:;" class="btnSave" data-name="@QuanLyGiaoVuService.Utilities.ThamSoConstantUtility.NHAPDIEMDONGKETTHUC" data-fieldName="EndRowIndex">Lưu làm mặc định</a>
            }
            </td>
            <td>@Html.ValidationMessageFor(model => model.EndRowIndex)</td>
        </tr>
        <tr class="setting">
            <th class="formLabel">@Html.LabelFor(model => model.IdentityColumnIndex)</th>
            <td>@Html.TextBoxFor(model => model.IdentityColumnIndex)
                 @if (laGiaoVuKhoa)
            {
                    <a href="javascript:;" class="btnSave" data-name="@QuanLyGiaoVuService.Utilities.ThamSoConstantUtility.NHAPDIEMCOTMASOSINHVIEN" data-fieldName="IdentityColumnIndex">Lưu làm mặc định</a>
            }
            </td>
            <td>@Html.ValidationMessageFor(model => model.IdentityColumnIndex)</td>
        </tr>
        <tr class="setting">
            <th class="formLabel">@Html.LabelFor(model => model.ValueColumnIndex)</th>
            <td>@Html.TextBoxFor(model => model.ValueColumnIndex)
                 @if (laGiaoVuKhoa)
            {
                    <a href="javascript:;" class="btnSave" data-name="@QuanLyGiaoVuService.Utilities.ThamSoConstantUtility.NHAPDIEMCOTDIEMHOCPHAN" data-fieldName="ValueColumnIndex">Lưu làm mặc định</a>
            }
            </td>
            <td>@Html.ValidationMessageFor(model => model.ValueColumnIndex)</td>
        </tr>
    </table>
    @Html.HiddenFor(model => model.MaTapTin)
    <input type="hidden" id="filename" value="" />
    <input type="submit" id="btnSubmitDuLieu" style="display: none" />
  }}

<form id="uploadFile" name="UploadFile" action="/QuanLySinhVien/UploadFile" method="post">
    <table>
        <tr>
            <th class="formLabel">File dữ liệu:</th>
            <td>
                <input type="file" name="fileTaiLieu" id="file" />
            </td>
        </tr>
    </table>
    <div id="loadingImage" class="hiddenField ui-overlay">
        <img src="~/Images/gif/loading-bar.gif" />
    </div>

    <input type="submit" id="uploadFileSubmit" style="display: none;" />
</form>

<input type="submit" id="submitTaoTaiLieu" value="Nhập Điểm" />

<div id="chonLopMonHocDialog"></div>

<script>
    $(document).ready(function () {
        $(".setting").addClass("hiddenField");

        $("input:file").change(function () {
            $("#filename").val($(this).val());
        });

        $("#StartRowIndex").spinner({
            min: 1
        });
        $("#EndRowIndex").spinner({
            min: 1
        });

        $("#TuyChinh").bind("change", function () {
            if ($(this).is(":checked")) {
                $(".setting").each(function () {
                    $(this).removeClass("hiddenField");
                });
            }
            else {
                $(".setting").each(function () {
                    if (!$(this).hasClass("hiddenField"))
                        $(this).addClass("hiddenField");
                });
            }
        });

        $("#uploadFile").ajaxForm({
            dataType: 'json',
            success: uploadFileSuccess,
            beforeSubmit: beforeUploadFile,
            error: uploadFileFail,
            uploadProgress: showProgress
        });

        $("#submitTaoTaiLieu").button().bind("click", SubmitNhapDuLieu);

        $("#TenLopMonHoc").bind("click", function () {
            var maLopSinhVienParam = "maLopSinhVien=" + $("#MaLopSinhVien").val();
            var maHocKyParam = "maHocKy=" + $("#MaHocKy").val();
            var maGiangVienParam = "maGiangVien=" + $("#MaGiangVien").val();
            var param = "?" + maLopSinhVienParam + "&" + maHocKyParam + "&" + maGiangVienParam;
            $("#chonLopMonHocDialog").load("/QuanLySinhVien/DanhSachLopMonHoc" + param, function () {
                $("#chonLopMonHocDialog").dialog({
                    title: "Chọn lớp",
                    autoOpen: true,
                    width: 800,
                    height: 600,
                    buttons: {
                        "Chọn": function () {
                            if (DanhSachLopMonHocObject != null) {
                                if (DanhSachLopMonHocObject.SelectedId != null) {
                                    $("#MaLopMonHoc").val(DanhSachLopMonHocObject.SelectedId);
                                    $("#TenLopMonHoc").html(DanhSachLopMonHocObject.GetMaLopVaTenMonHoc());
                                    $("#chonLopMonHocDialog").dialog("close");
                                }
                            }
                        }
                    }
                });
            });
        });

        $(".btnSave").bind("click", function () {
            var tenThamSo = $(this).data("name");
            var id = $(this).data("fieldname");
            var giatri = $("#" + id).val();
            $.ajax({
                type: "post",
                url: "/CaiDat/LuuThamSo",
                data: {
                    tenThamSo: tenThamSo,
                    giaTri: giatri
                },
                success: function (data) {
                    if (data.success) {
                        ToastManager.showSuccessToast("Lưu thành công.");
                    }
                    else {
                        ToastManager.showStickyWarningToast(data.message);
                    }
                }
            });
        });
    });

    function uploadFileSuccess(data) {
        $("#loadingImage").addClass("hiddenField");
        $("#MaTapTin").val(data.fileId);
        $("#TenTapTin").val(data.fileName);
        $("#KichThuoc").val(data.fileSize);
        $("#btnSubmitDuLieu").click();
    }

    function uploadFileFail() {
        $("#loadingImage").addClass("hiddenField");
        ToastManager.showStickyWarningToast("Không thể upload file.");
    }

    function showProgress(event, position, total, percentComplete) {
        $("#position").val(position);
    }

    function SubmitNhapDuLieu() {
        if ($("#filename").val().length > 0)
            $("#uploadFileSubmit").click();
        else
            ToastManager.showStickyWarningToast("Chưa chọn file.");
    }

    function beforeUploadFile() {
        $("#loadingImage").removeClass("hiddenField");
    }

    function OnSuccessCallBack(data) {
        if (data.success) {
            ToastManager.showSuccessToast("Nhập dữ liệu thành công.");
            var myTimeout = setTimeout(function () {
                location.reload(true);
            }, 1000);
        }
        else
            ToastManager.showStickyWarningToast(data.message);
    }
</script>
